export default class Verify {
  // 验证为空
  static isnull(str) {
    return str.length === 0 || str == null || str === ''
  }
  // 验证手机号
  static isPhone(str) {
    return /^(1(\d{10}))|(09(\d{8}))$/.test(str)
  }

  static isPhoneTips(phone) {
    console.log(this.isNull(phone))
    const reg = /^(1(\d{10}))|(09(\d{8}))$/
    if (this.isnull(phone)) {
      return '手机号不能为空'
    } else if (!reg.test(phone)) {
      return '手机号格式不正确'
    }
  }
  static isPwd(str) {
    return /^[\w]{6,12}$/.test(str)
  }

  static isPwdTips(pwd, tips) {
    const reg = /^[\w]{6,12}$/
    if (this.isnull(pwd)) {
      return tips + '不能为空'
    } else if (!reg.test(pwd)) {
      return tips + '格式不正确'
    }
  }

  // 验证身份证
  static isSFZ(sfz) {
    return /(^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$)|(^[1-9]\d{5}\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}$)/.test(sfz)
  }
  // 验证银行卡
  static isYHK(yhk) {
    return /^([\d]{4})([\d]{4})([\d]{4})([\d]{4})([\d]{0,})?/.test(yhk)
  }

  static isEmail(email) {
    return /\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/.test(email)
  }
  // 获取localstorage
  static getStorage(key) {
    return window.localStorage.getItem(key) == 'undefined' ? "": JSON.parse(window.localStorage.getItem(key))
  }
  // 设置localstorage
  static setStorage(key, v) {
    window.localStorage.setItem(key, JSON.stringify(v))
  }
  // 添加新key到localstorage
  static addStorage(key, addedV) {
    let oldVal = this.getStorage(key)
    let newVal = Object.assign(oldVal, addedV)
    console.log(newVal)
    this.setStorage(key, newVal)
  }
  // 移除localstorage
  static removeStorage(key) {
    window.localStorage.removeItem(key)
  }

  static isNull(form, vue) {
    var flag = true
    console.log(form, vue)
    vue.$refs[form].validate((valid) => {
      console.log(valid)
      if (valid || valid === 0 || valid === '0') {
        flag = true
      } else {
        flag = false
        // vue.$message.error('必填项为空或格式不正确');
      }
    })
    return flag
  }

  static isNull1(form, vue) {
    var flag = true
    vue.$refs[form][0].validate((valid) => {
      if (valid || valid === 0 || valid === '0') {
        flag = true
      } else {
        flag = false
      }
    })
    return flag
  }

  static vailNull(form, arr, vue) {
    var flag = true
    for (let i = 0; i < arr.length; i++) {
      if (!arr[i].prop) {
        flag = false
        vue.$message.error(arr[i].tips + '不能为空！');
        vue.$refs[form].validate()
        return;
      }
    }
    return flag
  }

  // 获取表格高度
  static getTableHeight(num) {
    var clientHeight = 0
    if (document.body.clientHeight && document.documentElement.clientHeight) {
      clientHeight = (document.body.clientHeight < document.documentElement.clientHeight) ? document.body.clientHeight : document.documentElement.clientHeight
    } else {
      clientHeight = (document.body.clientHeight > document.documentElement.clientHeight) ? document.body.clientHeight : document.documentElement.clientHeight
    }
    return clientHeight - num
  }
}